package com.ling.module1sbbyyds.controller.chat;

import com.alibaba.fastjson.JSON;
import com.ling.sbbyyds.entity.news.ChatReceiveParams;
import com.ling.module1sbbyyds.service.chat.ClientChatService;
import com.ling.sbbyyds.interaction.ResultVo;
import com.ling.sbbyyds.interaction.chatgpt.RobotParam;
import com.ling.sbbyyds.interaction.chatgpt.RobotResponseData;
import com.ling.sbbyyds.request.robot.RobotGPT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;

import java.math.BigDecimal;
import java.util.Random;


/**
 * @Description: 类描述
 * @author: ling
 * @date: 2023年03月24日
 */
@RestController
@RequestMapping("api/chat/isLogin")
public class ClientChatController {
    @Autowired
    private RobotGPT robotGPT;
    Random random = new Random();
    @Autowired
    private ClientChatService clientChatService;


    /**
     * 用户发送信息
     * @param chatReceiveParams 发送信息
     * @return 操作反馈
     */
    @PostMapping("clientSendMsg")
    public ResultVo clientSendMsg(@RequestBody ChatReceiveParams chatReceiveParams){
       return clientChatService.clientSendMsg(chatReceiveParams);
    }

    /**
     * 用户创建群聊
     * @param chatReceiveParams 操作数据
     * @return 操作反馈
     */
    @PostMapping("createGroupChatRoom")
    public ResultVo createGroupChatRoom(@RequestBody ChatReceiveParams chatReceiveParams){
        return clientChatService.createGroupChatRoom(chatReceiveParams);
    }

    @PostMapping("GPT")
    public String gpt(String mes){
        int maxToken = random.nextInt(90) + 10;
        Float temperature = (random.nextInt(4) + 1) / 10f;
        RobotParam robotParam = new RobotParam();
        robotParam.setPrompt("你是API模型吗？")
                .setMax_tokens(maxToken)
                .setTemperature(new BigDecimal(temperature.toString()));
        RobotResponseData robotResponseData = robotGPT.robotQuestions(robotParam);
        StringBuilder stringBuilder = new StringBuilder();
        //robotResponseData.getChoices().forEach(item -> {
        //    stringBuilder.append(item.getText());
        //});
        return JSON.toJSONString(stringBuilder);
    }
}
